/*************************************************
Author: byron 
Date: 12/09/2013
Description:
Notes:
*************************************************/

#ifndef __CFightEndMgr_h__
#define __CFightEndMgr_h__

#include "CFightEndData.h"
#include "CTArray.h"

class CActCfgData;
class CPlayerBaseInfo;
class CPlayerOnlineData;
class CPlayerDetailInfoEx1;
class MsgAnsGMPlayer;

class CFightEndMgr
{
public:
    enum CMD_FIGHT_END_SUB_TAG
    {
        CMD_FIGHT_END_SUB_WAIT = 1,        //等待信息
        CMD_FIGHT_END_SUB_BATTLE = 2,      //战斗进行
        CMD_FIGHT_END_SUB_RESULT = 3,      //战斗结算
    };

    enum CMD_INNER_FIGHT_END_SUB_TAG
    {
        CMD_INNER_FIGHT_END_SEND_INFO = 1,        //下发信息
        CMD_INNER_FIGHT_END_CHECK_BAG = 2,        //检查背包
        CMD_INNER_FIGHT_END_LEAVE_MAP = 3,        //移出地图
        CMD_INNER_FIGHT_END_PREPARE_DATA = 4,     //准备数据
        CMD_INNER_FIGHT_END_LEAVE_MAP_NO_ANS = 5, //移出地图
    };

    enum ENM_FIGHT_END_MACRO_TAG
    {
        MACRO_FIGHT_END_ENTER_BAG_NUM = 3, //进入时背包格子数
        MACRO_FIGHT_END_BATTLE_BAG_NUM = 1,//战斗前背包格子数
        MACRO_FIGHT_END_MAX_AWARD_TIME = 3,//每场活动奖励次数
    };

    CFightEndMgr()
    {
        Clear();
    }

    void Clear()
    {
        _uiMapID = 0;
        _uiRelayMapID = 0;
        ClearRoleData();
    }

    void ClearRoleData()
    {
        _auiRoleList.m_iUsedCount = _auiRoleList.GetCount();
        for (int i = 0; i < _auiRoleList.Size(); ++i)
        {
            _auiRoleList[i] = 0;
        }

        _bIsNeedClearMap = false;
    }

    virtual ~CFightEndMgr()
    {
    }

    unsigned int GetMapID();
    unsigned int GetRelayMapID();

    bool IsNeedClearMap() const {return _bIsNeedClearMap;}
    void SetNeedClearMap(bool bVal) {_bIsNeedClearMap = bVal;}

    int GetMapPos(int iIndex, unsigned short& usPosX, unsigned short& usPosY);

    int SetMapPos(unsigned int uiMapID, int& iPosIndex, unsigned short& usPosX,
            unsigned short& usPosY, CPlayerBaseInfo& stPlayerBaseInfo);

    void RoleMoveOut(unsigned int uiGID);

    int ActOpen(CActCfgData& stActCfgData);
    int ActClose(CActCfgData& stActCfgData);

    int EnterScene(unsigned int uiMapID, unsigned short& usPosX,
            unsigned short& usPosY, CPlayerBaseInfo& stPlayerBaseInfo,
            CPlayerOnlineData& stPlayerOnlineData);

    void UpdateFightEndData(CPlayerDetailInfoEx1& stPlayerDetailEx1);

    int CheckFightEndData(CPlayerDetailInfoEx1& stPlayerDetailEx1);

    int MoveOutRole(unsigned int uiGID);

    int MoveOutRoleNoAns(unsigned int uiGID);

    void CheckBagGrid();

    void TriggerBattle();

    int DealBattle(CFightEndData& stRolesData, bool& bIsLeftWin, int& iWinIndex,
            bool& bIsReverWin);

    int DealBattle(CFightEndData& stRolesData, int iLeftIndex, int iRightIndex,
            int iBattleIndex, int iActType, bool& bLeftWinThisTime);

    int BattleReport(CPlayerBaseInfo& stPlayerBase,
            CPlayerDetailInfoEx1& stPlayerDetailEx1,
            CPlayerOnlineData& stPlayerOnlineData, bool bIsLeft,
            bool bIsBattleWin);

    int MoveOutRoleFromMap(unsigned int uiMapID, CPlayerBaseInfo& stPlayerBase,
            CPlayerOnlineData& stPlayerOnlineData);

    int MoveOutRoleWithoutMoveAns(unsigned int uiMapID,
            CPlayerBaseInfo& stPlayerBase,
            CPlayerOnlineData& stPlayerOnlineData);

    int GMDealFightEnd(CPlayerDetailInfoEx1& stPlayerDetailEx1,
            MsgAnsGMPlayer& stAns);
private:
    bool _bIsNeedClearMap;
    unsigned int _uiMapID;
    unsigned int _uiRelayMapID;
    CTLib::CTArray<unsigned int, MAX_FIGHT_END_ROLE_TOTAL> _auiRoleList;
};

#endif //__CFightEndMgr_h__

